﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Marmat.Foundation;


namespace Marmat.Template.WinForm
{
    public class EditFormPanelContainer : System.Windows.Forms.UserControl, Marmat.Foundation.IEditFormPanelContainer
    {
        private IEditFormPanel[] panelCollection;
        private IEditFormPanel selection;
        private IEditFormPanel mousePanel;
        private Dictionary<IEditFormPanel, Rectangle> panelRectangleCollection = new Dictionary<IEditFormPanel, Rectangle>();

        public EditFormPanelContainer()
        {
            SetStyle(ControlStyles.Selectable, true);
            SetStyle(ControlStyles.ResizeRedraw, true); //尺寸变化后绘制
            SetStyle(ControlStyles.Opaque, false); //改善绘图质量
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
            InitializeComponent();
        }

        public void Builder(Control owner, IEditFormController controller, IEnumerable<IEditFormPanel> panelCollection)
        {
            this.Dock = DockStyle.Right;
            this.panelCollection = panelCollection.ToArray();
            foreach (var panel in this.panelCollection)
            {
                if (panel is System.Windows.Forms.Control control)
                {
                    control.Dock = DockStyle.Fill;
                    this.Controls.Add(control);
                }
                if (panelCollection.Count() > 0)
                {
                    this.selection = this.panelCollection[0];
                    this.Invalidate();
                }
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            this.panelRectangleCollection.Clear();
            int left = this.Padding.Left;
            e.Graphics.DrawLine(new Pen(Color.FromArgb(238, 238, 238)), 0, this.Padding.Top - 1, this.Width, this.Padding.Top - 1);
            foreach (var panel in this.panelCollection)
            {
                if (panel is System.Windows.Forms.Control control)
                {
                    var size = TextRenderer.MeasureText(control.Text, this.Font);
                    Rectangle rectangle = new Rectangle(left, 6, size.Width + 6, this.Padding.Top - 6);
                    Pen pen = new Pen(Color.Gray, 2);
                    if (this.selection == panel || this.mousePanel == panel)
                    {
                        pen.Color = Color.FromArgb(52, 143, 228);
                        e.Graphics.DrawLine(pen, rectangle.X, rectangle.Bottom - 2, rectangle.Right, rectangle.Bottom - 2);
                    }
                    TextRenderer.DrawText(e.Graphics, control.Text, this.Font, rectangle, pen.Color, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
                    panelRectangleCollection[panel] = rectangle;
                    left += rectangle.Width + 24;
                }
            }
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            var panel = this.panelRectangleCollection.FirstOrDefault(x => x.Value.Contains(e.Location)).Key;
            if (panel is not null && panel is Control control)
            {
                this.selection = panel;
                control.BringToFront();
                this.Invalidate();
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            var panel = this.panelRectangleCollection.FirstOrDefault(x => x.Value.Contains(e.Location)).Key;
            this.Cursor = panel is null ? Cursors.Default : Cursors.Hand;
            if (panel != this.mousePanel)
            {
                this.mousePanel = panel;
                this.Invalidate();
            }
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.mousePanel = null;
            this.Cursor = Cursors.Default;
            this.Invalidate();
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // EditFormPanelContainer
            // 
            this.BackColor = System.Drawing.Color.White;
            this.Font = new System.Drawing.Font("微软雅黑", 10.5F);
            this.Name = "EditFormPanelContainer";
            this.Padding = new System.Windows.Forms.Padding(12, 50, 12, 0);
            this.Size = new System.Drawing.Size(400, 300);
            this.ResumeLayout(false);

        }


    }
}
